package thread_pool;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/19
 * Time:20:46
 * 创建可执行定时任务的线程池
 **/
public class PoolDemo6 {
    public static void main(String[] args) {
        //创建可执行定时任务的线程池
        ScheduledExecutorService service = Executors.newScheduledThreadPool(10);//表示创建线程的个数最多10个                    System.out.println("执行任务"+new Date());
        System.out.println("执行任务"+new Date());//执行任务前
        //执行任务
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("执行任务"+new Date());
            }
        },1,3, TimeUnit.SECONDS);
    }
}
